import { message } from 'ant-design-vue'
import { isDef } from '@/core/utils/Common'
/**
 * 通用的表单重置、保存功能
 * 参数
 * options: {state: null, remoteFetchData: '请求接口', formState: null, original: null}
 */
export default function (options) {

  // 初始化数据Initalizer 初始化
  const InitializeData = async (primaryKey) => {
    options.state.visible = true
    options.state.loading = true
    Object.assign(options.formState, options.original)
    //
    if (isDef(primaryKey)) {
      try{
        let res = await options.remoteFetchData(primaryKey)
        if (res.code == 0) {
          Object.assign(options.formState, res.data)
        }
      }catch(err){
        JSON.stringify(err)
        message.error(JSON.stringify(err), 2, () => {
          options.state.visible = false
        })
      }
      options.state.loading = false
    } else {
      console.error('请选要查看的记录', 2, ()=>{
        options.state.loading = false
        options.state.visible = false
      })
    }
  }

  return {InitializeData}
}